package jangdolbaeng2.customer.action;

import jangdolbaeng2.customer.dao.CartDAO;
import jangdolbaeng2.customer.dao.CustomerDAO;
import jangdolbaeng2.customer.dao.ProductDAO;
import jangdolbaeng2.vo.CustomerVO;
import jangdolbaeng2.vo.DeliveryVO;
import jangdolbaeng2.vo.Order_detailVO;
import jangdolbaeng2.vo.ProductVO;
import jangdolbaeng2.vo.Product_commentVO;
import jangdolbaeng2.vo.RealCartVO;

import java.io.BufferedReader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import net.sf.json.JSONObject;
import oracle.sql.CLOB;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class ProductAction extends ActionSupport implements SessionAware {
	private Map<String, Object> session;
	private String category;
	private List<HashMap<String,Object>> product_list;
	private List<HashMap<String,Object>> md_list;
	private List<HashMap<String,Object>> md10list;
	private int product_seq;
	private HashMap<String, Object> product_vo;
	private List<Product_commentVO> comment_list;
	private String content;
	private int cmt_seq;
	private HashMap<String, Object> comment;
	private Product_commentVO comment_vo;
	private String customer_id;
	private int list_size;
	private List<String> customer_id_list;
	private boolean result;
	private String blog_photo;
	private List<HashMap<String, Object>> result_category_list;
	private String keyword;
	private HashMap<String, Object> keywords;
	
	//return
	//product_list.jsp
	
	public String getAllMDList(){
		
		md_list = new ProductDAO().getMDList();
		System.out.println("MD리스트");
		
		for(int i = 0; i <md_list.size();i++ ){
			HashMap<String, Object> map = new HashMap<String, Object>();
			map = md_list.get(i);
			//CLOB 변환
			if(map.get("PRODUCT_IMAGE1")!=null){
				map.put("PRODUCT_IMAGE1",clobToString((CLOB)(map.get("PRODUCT_IMAGE1"))));
			}
			//
		}
		return SUCCESS;
	}
	
	public String selectAllProductCategory(){
		
		product_list = new ProductDAO().selectAllProductCategory(category);
		
		list_size = product_list.size();
		System.out.println(list_size + "개의 상품리스트");
		//CLOB변환
		for(int i = 0; i <product_list.size();i++ ){
			HashMap<String, Object> map = new HashMap<String, Object>();
			map = product_list.get(i);
			if((map.get("PRODUCT_IMAGE1")!=null))map.put("PRODUCT_IMAGE1",clobToString((CLOB)(map.get("PRODUCT_IMAGE1"))));
		}
		return SUCCESS;
	}
	
	public String selectAllProductQuantity(){
		
		product_list = new ProductDAO().selectAllProductQuantity(category);
		
		list_size = product_list.size();
		System.out.println(list_size + "개의 상품리스트");
		//CLOB변혼
		for(int i = 0; i <product_list.size();i++ ){
			HashMap<String, Object> map = new HashMap<String, Object>();
			map = product_list.get(i);
			map.put("PRODUCT_IMAGE1",clobToString((CLOB)(map.get("PRODUCT_IMAGE1"))));
		}
		
		return SUCCESS;
	}
	
	public String selectAllProductLowPrice(){
		product_list = new ProductDAO().selectAllProductLowPrice(category);
		
		list_size = product_list.size();
		System.out.println(list_size + "개의 상품리스트");
		//CLOB변혼
		for(int i = 0; i <product_list.size();i++ ){
			HashMap<String, Object> map = new HashMap<String, Object>();
			map = product_list.get(i);
			map.put("PRODUCT_IMAGE1",clobToString((CLOB)(map.get("PRODUCT_IMAGE1"))));
		}
		
		return SUCCESS;
	}
	
	public String selectAllProductHighPrice(){

		product_list = new ProductDAO().selectAllProductHighPrice(category);
		
		list_size = product_list.size();
		System.out.println(list_size + "개의 상품리스트");
		//CLOB변혼
		for(int i = 0; i <product_list.size();i++ ){
			HashMap<String, Object> map = new HashMap<String, Object>();
			map = product_list.get(i);
			map.put("PRODUCT_IMAGE1",clobToString((CLOB)(map.get("PRODUCT_IMAGE1"))));
		}
		
		list_size = product_list.size();
		System.out.println(list_size);
		
		return SUCCESS;
	}
	
	
	//product_detail.jsp
	public String selectAProduct(){
		product_vo = new ProductDAO().selectAProduct(product_seq);
		
		//CLOB 변환
		if(product_vo.get("PRODUCT_IMAGE1")!=null){
			product_vo.put("PRODUCT_IMAGE1", clobToString((CLOB)product_vo.get("PRODUCT_IMAGE1")));
		}
		if(product_vo.get("PRODUCT_IMAGE2")!=null){
		product_vo.put("PRODUCT_IMAGE2", clobToString((CLOB)product_vo.get("PRODUCT_IMAGE2")));
		}
		
		System.out.println(product_vo.get("PRODUCT_SEQ"));
		
		comment_list = new ProductDAO().selectCommentList(product_seq);
		System.out.println(comment_list);
		
		customer_id_list = new ProductDAO().selectAllCustomer(product_seq);
		customer_id = (String)session.get("customer_id");
		
		result = false;
		
		if(customer_id_list!=null&&customer_id!=null){ 
			for (int i = 0; i < customer_id_list.size(); i++) {
				if(customer_id.equals(customer_id_list.get(i))){
					result = true;
				}
			}
		}
		
		CLOB clob = (CLOB)new ProductDAO().selectAShopBlog_photo(Integer.parseInt(String.valueOf(product_vo.get("SHOP_SEQ"))));
		if(clob!=null){
			blog_photo = clobToString(clob);
		}

		return SUCCESS;
 	}
	
	//editProductComment
	public String editProductComment(){
		
//		System.out.println(content);
//		System.out.println(cmt_seq);
		
		comment = new HashMap<String, Object>();
		comment.put("content", content);
		comment.put("cmt_seq", cmt_seq);
		
		new ProductDAO().editProductComment(comment);
		
		comment_vo = new ProductDAO().selectAComment(cmt_seq);
		
		return SUCCESS;
	}
	
	//deleteProductComment
	public String deleteProductComment(){
		product_seq = new ProductDAO().selectAComment(cmt_seq).getProduct_seq();
		System.out.println(product_seq);
		
		new ProductDAO().deleteProductComment(cmt_seq);
		System.out.println(cmt_seq +"comment 삭제");

		comment_list = new ProductDAO().selectCommentList(product_seq);
		
		return SUCCESS;
	}
	
	//insertProductComment
	public String  insertProductComment(){
		
		customer_id = (String)session.get("customer_id");
		
		comment_vo = new Product_commentVO();
		comment_vo.setContent(content);
		comment_vo.setProduct_seq(product_seq);
		comment_vo.setCustomer_id(customer_id);
		
		new ProductDAO().insertProductComment(comment_vo);
		
		System.out.println("입력: " + comment_vo);
		
		comment_list = new ProductDAO().selectCommentList(product_seq);
		System.out.println(comment_list);
		
		return SUCCESS;
	}
	
	public String goSearch(){
		keyword = "%"+keyword+"%";
		
		System.out.println(keyword);
		product_list = new ProductDAO().selectAllforSearching(keyword);
		
		for (int i = 0; i < product_list.size(); i++) {
			CLOB clob = (CLOB)product_list.get(i).get("PRODUCT_IMAGE1");
			String str = clobToString(clob);
			product_list.get(i).put("PRODUCT_IMAGE1", str);
		}
		//category별!
		result_category_list = new ProductDAO().selectResultforSearching(keyword);
		
		System.out.println(keyword);
		System.out.println("카테고리별" + result_category_list);
		return SUCCESS;
	}
	
	public String getSearchCategory(){
		if(keyword == null || category == null) return SUCCESS;
		keyword = "%"+keyword+"%";
		System.out.println(keyword +"/"+category +"검색하기");
		
		keywords = new HashMap<String, Object>();
		keywords.put("keyword", keyword);
		keywords.put("category", category);
		product_list = new ProductDAO().getSearchCategory(keywords);
		for (int i = 0; i < product_list.size(); i++) {
			CLOB clob = (CLOB)product_list.get(i).get("PRODUCT_IMAGE1");
			String str = clobToString(clob);
			product_list.get(i).put("PRODUCT_IMAGE1", str);
		}
		
		result_category_list = new ProductDAO().selectResultforSearching(keyword);

		return SUCCESS;
	}
	
	//getter setter
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	public String getCategory() {
		return category;
	}
	public void setCategory(String category) {
		this.category = category;
	}
	public List<HashMap<String, Object>> getProduct_list() {
		return product_list;
	}
	public void setProduct_list(List<HashMap<String, Object>> product_list) {
		this.product_list = product_list;
	}
	public List<HashMap<String, Object>> getMd_list() {
		return md_list;
	}
	public void setMd_list(List<HashMap<String, Object>> md_list) {
		this.md_list = md_list;
	}
	public int getProduct_seq() {
		return product_seq;
	}
	public void setProduct_seq(int product_seq) {
		this.product_seq = product_seq;
	}
	public HashMap<String, Object> getProduct_vo() {
		return product_vo;
	}
	public void setProduct_vo(HashMap<String, Object> product_vo) {
		this.product_vo = product_vo;
	}
	public List<Product_commentVO> getComment_list() {
		return comment_list;
	}
	public void setComment_list(List<Product_commentVO> comment_list) {
		this.comment_list = comment_list;
	}
	public int getCmt_seq() {
		return cmt_seq;
	}
	public void setCmt_seq(int cmt_seq) {
		this.cmt_seq = cmt_seq;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public HashMap<String, Object> getComment() {
		return comment;
	}
	public void setComment(HashMap<String, Object> comment) {
		this.comment = comment;
	}
	public Product_commentVO getComment_vo() {
		return comment_vo;
	}
	public void setComment_vo(Product_commentVO comment_vo) {
		this.comment_vo = comment_vo;
	}
	public String getCustomer_id() {
		return customer_id;
	}
	public void setCustomer_id(String customer_id) {
		this.customer_id = customer_id;
	}
	public int getList_size() {
		return list_size;
	}
	public void setList_size(int list_size) {
		this.list_size = list_size;
	}
	public List<String> getCustomer_id_list() {
		return customer_id_list;
	}
	public void setCustomer_id_list(List<String> customer_id_list) {
		this.customer_id_list = customer_id_list;
	}
	public boolean isResult() {
		return result;
	}
	public void setResult(boolean result) {
		this.result = result;
	}
	public String getBlog_photo() {
		return blog_photo;
	}
	public void setBlog_photo(String blog_photo) {
		this.blog_photo = blog_photo;
	}
	public List<HashMap<String, Object>> getMd10list() {
		return md10list;
	}
	public void setMd10list(List<HashMap<String, Object>> md10list) {
		this.md10list = md10list;
	}
	public List<HashMap<String, Object>> getResult_category_list() {
		return result_category_list;
	}
	public void setResult_category_list(
			List<HashMap<String, Object>> result_category_list) {
		this.result_category_list = result_category_list;
	}
	public String getKeyword() {
		return keyword;
	}
	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	//clob 변환하는 메소드!
	 public String clobToString(CLOB clob) {
		StringBuffer s = new StringBuffer();
        try {
	        BufferedReader br = new BufferedReader(clob.getCharacterStream());
	        String ts = "";
	        while((ts = br.readLine()) != null) {
	               s.append(ts + "\n");
	        }
	        br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
        return s.toString();
	 }
	
	
	
	
	
	
	
	
	
	//-----------customer액션에 합칠내용!!!!!!
	//변수 getter setter
	private List<HashMap<String, Object>> delivery_list;
	public List<HashMap<String, Object>> getDelivery_list() {
		return delivery_list;
	}
	public void setDelivery_list(List<HashMap<String, Object>> delivery_list) {
		this.delivery_list = delivery_list;
	}
	private HashMap<String, Object> order_vo;
	public HashMap<String, Object> getOrder_vo() {
		return order_vo;
	}
	public void setOrder_vo(HashMap<String, Object> order_vo) {
		this.order_vo = order_vo;
	}
	private int order_seq;
	public int getOrder_seq() {
		return order_seq;
	}
	public void setOrder_seq(int order_seq) {
		this.order_seq = order_seq;
	}
	private List<HashMap<String, Object>> product_name_list; //name이랑 quantity저장되는 hashmap
	public List<HashMap<String, Object>> getProduct_name_list() {
		return product_name_list;
	}
	public void setProduct_name_list(List<HashMap<String, Object>> product_name_list) {
		this.product_name_list = product_name_list;
	}
	private String product_name;
	public String getProduct_name() {
		return product_name;
	}
	public void setProduct_name(String product_name) {
		this.product_name = product_name;
	}
	private DeliveryVO delivery_vo;
	public DeliveryVO getDelivery_vo() {
		return delivery_vo;
	}
	public void setDelivery_vo(DeliveryVO delivery_vo) {
		this.delivery_vo = delivery_vo;
	}
	private int quantity;
	public int getQuantity() {
		return quantity;
	}
	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}
	private List<HashMap<String, Object>> cartList;
	public List<HashMap<String, Object>> getCartList() {
		return cartList;
	}
	public void setCartList(List<HashMap<String, Object>> cartList) {
		this.cartList = cartList;
	}
	private HashMap<String, Object> cart_vo;
	public HashMap<String, Object> getCart_vo() {
		return cart_vo;
	}
	public void setCart_vo(HashMap<String, Object> cart_vo) {
		this.cart_vo = cart_vo;
	}
	private CustomerVO customer;
	public CustomerVO getCustomer() {
		return customer;
	}
	public void setCustomer(CustomerVO customer) {
		this.customer = customer;
	}
	public HashMap<String, Object> getKeywords() {
		return keywords;
	}
	public void setKeywords(HashMap<String, Object> keywords) {
		this.keywords = keywords;
	}
	private List<RealCartVO>  realCart;
	private RealCartVO realCartVO;
	public List<RealCartVO> getRealCart() {
		return realCart;
	}
	public void setRealCart(List<RealCartVO> realCart) {
		this.realCart = realCart;
	}
	public RealCartVO getRealCartVO() {
		return realCartVO;
	}
	public void setRealCartVO(RealCartVO realCartVO) {
		this.realCartVO = realCartVO;
	}
	private ProductVO product;
	public ProductVO getProduct() {
		return product;
	}
	public void setProduct(ProductVO product) {
		this.product = product;
	}

	//return
	public String goDelivery(){
		
		customer_id = (String)session.get("customer_id");
		System.out.println(customer_id);
		
		delivery_list = new ProductDAO().selectAllDelivery(customer_id);
		list_size = delivery_list.size();
		System.out.println(delivery_list);
		//이름 입력
		if(delivery_list==null){
			System.out.println("주문내역null");
			return SUCCESS;
		}
		for(int i = 0; i < delivery_list.size();i++){
			//
			order_seq = Integer.parseInt(String.valueOf(delivery_list.get(i).get("ORDER_SEQ")));
			System.out.println("주문번호" + order_seq);
			//
			product_name_list = new ProductDAO().selectAllProduct_name(order_seq);
			for(int j = 0; j< product_name_list.size();j++){
				String str = clobToString((CLOB)product_name_list.get(j).get("PRODUCT_IMAGE1"));
				product_name_list.get(j).put("PRODUCT_IMAGE1", str);
			}
			//
			delivery_list.get(i).put("PRODUCT_NAME_LIST", product_name_list);
			System.out.println(delivery_list.get(i));
		}
		//
		return SUCCESS;
	}
	public String goDeliveryDetail(){
		//배송정보
		order_vo = new ProductDAO().selectAOrder(order_seq);
		System.out.println(order_vo);
		
		product_name_list = new ProductDAO().selectAllProduct_name(order_seq);
		for(int i = 0; i< product_name_list.size();i++){
			String str = clobToString((CLOB)product_name_list.get(i).get("PRODUCT_IMAGE1"));
			product_name_list.get(i).put("PRODUCT_IMAGE1", str);
		}
		System.out.println(product_name_list);
		
		delivery_vo = new ProductDAO().selectADelivery(order_seq);
		System.out.println(delivery_vo);
		
		return SUCCESS;
	}
	public String goOrderPage() throws SQLException{
		customer_id = (String)session.get("customer_id");
		
		product_vo = new ProductDAO().selectAProduct(product_seq);
		System.out.println(product_vo);
		
	    customer = new CustomerDAO().getCustomer(customer_id);
	      
	    realCart = new ArrayList<RealCartVO>(); 
	      
	    realCartVO = new RealCartVO();
	    realCartVO.setCustomer_id(customer_id);
	         
	    product = new CartDAO().getProduct(product_seq);
	         
	    realCartVO.setProduct_seq(product_seq);
	    realCartVO.setQuantity(quantity);
	    realCartVO.setProduct_name(product.getProduct_name());
	    realCartVO.setProduct_image1(product.getProduct_image1());
	    realCartVO.setProduct_image2(product.getProduct_image2());
	    realCartVO.setProduct_price(product.getProduct_price());
	         
	    realCart.add(realCartVO);
		
	    session.put("realCart", realCart);
		return SUCCESS;
	}
	
}
